package com.egoo.ticket.server.project.sunong;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.egoo.ticket.server.dao.EwsDepartmentDao;
import com.egoo.ticket.server.pojo.entity.EwsUser;
import com.egoo.ticket.server.pojo.vo.EwsDepartmentVo;
import com.egoo.ticket.server.project.ProjectConstant;
import com.egoo.ticket.server.utils.common.ConstantCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Auther: victor_tang
 * @Date: 2020/10/15 10:49
 * @Description: 苏农定制基础方法
 */
@Component
@Slf4j
public class SuNongBaseUtil {
	@Autowired
	private EwsDepartmentDao ewsDepartmentDao;

	private static final String URGE_TEMPLATE_CODE = "16";
	private static final String TIME_OUT_TEMPLATE_CODE ="11";
	/**
	 * TODO
	 * @author: victor_tang
	 * @createtime: 2020/10/15 10:50
	 * @param customField 1
	 * @param ewsUser 2
	 * @return: com.alibaba.fastjson.JSONObject
	 */
	public JSONObject addHandleInsitution(JSONObject customField,
										  EwsUser ewsUser) {
		if (ewsUser==null||StringUtils.isEmpty(ewsUser.getDepartmentId())) {
			log.info(ewsUser.getName()+"用户所属部门为空，无法记录工单处理人涉及机构信息！");
			return customField;
		}
		if (customField==null) {
			customField = new JSONObject();
		}
		List<String> parentIdsByDeptId = getParentIdsByDeptId(ewsUser.getDepartmentId());
		String handelInsitution = StringUtils.join(parentIdsByDeptId, "/");

		String oldHandelInsitution = customField.getString(ProjectConstant.SuNongConstant.HANDLE_INSITUTION);
		if (StringUtils.isNotEmpty(oldHandelInsitution)) {
			handelInsitution = oldHandelInsitution+","+handelInsitution;
		}
		customField.put(ProjectConstant.SuNongConstant.HANDLE_INSITUTION,handelInsitution);
		return customField;
	}
	/**
	 * TODO 根据部门id获取其全链路部门id列表
	 * @author: victor_tang
	 * @createtime: 2020/10/15 10:56
	 * @param  deptId 1
	 * @return: java.lang.String
	 */
	public List<String> getParentIdsByDeptId(String deptId){
		List<String> ids = new ArrayList<>();
		List<String> reverseDeptIds = recursiveGetDeptId(deptId, ids);
		Collections.reverse(reverseDeptIds);
		reverseDeptIds.add(deptId);
		return reverseDeptIds;
	}

	/**
	 * TODO 递归获取部门所有
	 * @author: victor_tang
	 * @createtime: 2020/10/15 15:15
	 * @param deptId 1
	 * @param ids 2
	 * @return: java.util.List<java.lang.String>
	 */
	public List<String> recursiveGetDeptId(String deptId,List<String> ids){
		EwsDepartmentVo ewsDepartmentVo = ewsDepartmentDao.selectByPrimaryKey(deptId);
		if (ewsDepartmentVo!=null&&StringUtils.isNotEmpty(ewsDepartmentVo.getPid())) {
			EwsDepartmentVo pvo = ewsDepartmentDao.selectByPrimaryKey(ewsDepartmentVo.getPid());
			if (pvo!=null) {
				ids.add(ewsDepartmentVo.getPid());
				recursiveGetDeptId(ewsDepartmentVo.getPid(),ids);
			}
			return ids;
		}
		return ids;
	}

	public String getMsgTemplate(JSONArray template,String msgType){

		for (int i = 0; i <template.size() ; i++) {
			String code = template.getJSONObject(i).getString("code");
			String name = template.getJSONObject(i).getString("name");
			String msgTemplate = template.getJSONObject(i).getString("remark");
			if (ConstantCode.WorkOrderLogType.LOG_TYPE_URGE.equals(msgType) && URGE_TEMPLATE_CODE.equals(code)) {
				return msgTemplate;
			}
			if (ConstantCode.WorkOrderLogType.LOG_TYPE_TIME_OUT.equals(msgType) && TIME_OUT_TEMPLATE_CODE.equals(code)){
				return msgTemplate;
			}
		}
		return null;
	}
}
